﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class AndroidMoveController : MonoBehaviour
{
    private NavMeshAgent navMeshAgent;
    private Vector3 playerPos;
    private Transform aim = null;
    // Start is called before the first frame update
    void Start()
    {
        navMeshAgent = GetComponent<NavMeshAgent>();
        InvokeRepeating("GetPlayerPos", 1, 0.5f);
        aim = GameInfo.Instance.GetAndroidAim();
    }

    // Update is called once per frame
    void Update()
    {
        if(aim==null)
        {
            return;
        }
        if (aim!= null && Vector3.Distance(transform.position, aim.position) < 3)
        {
            navMeshAgent.enabled = false;
        }
        if (Vector3.Distance(transform.position, aim.position) >= 3 && !navMeshAgent.enabled)
        {
            navMeshAgent.enabled = true;
        }
        if (aim != null)
        {
            transform.LookAt(aim);
        }

    }
    void GetPlayerPos()
    {
        playerPos = GameInfo.Instance.GetPlayerPos();
        if (navMeshAgent.enabled&&gameObject.active)
        {
            navMeshAgent.SetDestination(playerPos);            
        }

        //Debug.Log(playerPos);
        aim = GameInfo.Instance.GetAndroidAim();
    }
}
